﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsCommonStorage
{
    internal static class ErrorHandler
    {
        internal static void Action(Action action)
        {
            try
            {
                action();
            }
            catch (IOException e)
            {
                throw e;

            }
            catch (ObjectDisposedException e)
            {
                throw e;
            }
            catch (Exception e)
            {
                throw new IOException(e.Message, e);
            }
        }

        internal static T Func<T>(Func<T> func)
        {
            try
            {
                return func();
            }
            catch (IOException e)
            {
                throw e;

            }
            catch (ObjectDisposedException e)
            {
                throw e;
            }
            catch (Exception e)
            {
                throw new IOException(e.Message, e);
            }
        }

        internal static async Task AsyncAction(Func<Task> func)
        {
            try
            {
                await func().ConfigureAwait(false);
            }
            catch (IOException e)
            {
                throw e;
            }
            catch (ObjectDisposedException e)
            {
                throw e;
            }
            catch (Exception e)
            {
                throw new IOException(e.Message, e);
            }
        }

        internal static async Task<T> AsyncFunc<T>(Func<Task<T>> func)
        {
            try
            {
                return await func().ConfigureAwait(false);
            }
            catch (IOException e)
            {
                throw e;
            }
            catch (ObjectDisposedException e)
            {
                throw e;
            }
            catch (Exception e)
            {
                throw new IOException(e.Message, e);
            }
        }
    }
}
